<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织机构" name="first" />
        </el-tabs>
        <div style="padding:30px 50px">
          <!-- 用一个行列布局 -->
          <el-row type="flex" justify="space-between" align="middle" style="height: 40px;border-bottom:1px solid #cfcfcf;margin-bottom:15px">
            <el-col :span="20">
              <svg-icon icon-class="home" /><span style="font-weight:600">江苏传智播客教育科技股份有限公司</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span>
                      操作<i class="el-icon-arrow-down" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item>添加子部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <!-- 放置一个el-tree组件 -->
          <el-tree
            :data="list"
            :default-expand-all="true"
          >
            <!-- 具体页面结构 -->
            <template slot-scope="{ data }">
              <!-- 用一个行列布局 -->
              <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
                <el-col :span="20">
                  <svg-icon icon-class="home" /><span>{{ data.name }}</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <!-- 两个内容 -->
                    <el-col>{{ data.manager }}</el-col>
                    <!-- 下拉菜单 element -->
                    <el-col>
                      <el-dropdown>
                        <span>
                          操作<i class="el-icon-arrow-down" />
                        </span>
                        <!-- 下拉菜单 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                          <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                          <el-dropdown-item @click.native="hRemove(data.id)">删除部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-tree>
        </div>

        <el-dialog
          title="编辑部门"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <addor-edit v-if="showDialog" :id="curId" :edit-id="editId" :visible.sync="showDialog" @success="hSuccess" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, removeDepartments } from '@/api/departments.js'
// 转树形结构
import { tranListToTreeData } from '@/utils/tree.js'
// 引入弹窗组件
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      showDialog: false, // 是否显示弹层
      list: [],
      activeName: 'first',
      curId: '',
      editId: ''
    }
  },
  watch: {
    showDialog: {
      deep: true,
      handler(change) {
        if (!change) {
          this.editId = null
          this.curId = null
        }
      }
    }
  },
  mounted() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        this.list = tranListToTreeData(res.data.depts)
      } catch (err) {
        console.log(err)
      }
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
    },
    hEdit(id) {
      this.showDialog = true
      this.editId = id
    },
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartments()
      this.editId = null
      this.curId = null
    },
    hRemove(id) {
      this.$confirm('亲，确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await removeDepartments(id)
        if (res.code === 1000) {
          this.$message({
            type: 'success',
            message: res.message
          })
          this.loadDepartments()
        } else {
          this.$message({
            type: 'error',
            message: '删除失败，请联系管理员！'
          })
        }
      }).catch(err => err)
    }
  }
}
</script>
